﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SpiderHelper.Tools.StockStar
{
    public class StockStarURLAnalysis
    {
        public string AnalysisURLContent(string url, string encode, int channelId, out bool enSured)
        {
            System.Net.CookieContainer container = new System.Net.CookieContainer();
            Common.Net.Request request = new Common.Net.Request(container);
            string html = "";
            try
            {
                html = request.ReadUrlContent(url, encode);
            }
            catch (Exception)
            {
                html = "";
            }
            if (!string.IsNullOrEmpty(html))
            {
                int pageIndex = html.IndexOf("<div id=\"Page\">");

                if (pageIndex < 0)//没有分页的内容，不然用起来麻烦
                {
                    int startIndex = html.IndexOf("<div class=\"article\" id=\"container-article\">");
                    html = html.Substring(startIndex + 44);
                    int endIndex = html.IndexOf("<div class=\"share\">");
                    html = html.Substring(0, endIndex);

                    endIndex = html.LastIndexOf("</div>");
                    html = html.Substring(0, endIndex);
                    html = Tools.CommonTool.GetNoHTMLText(html);
                    html = DropImgs(html);
                    //foreach (string s in query)
                    //{
                    //    html = html.Replace(s, url + "?url=" + s);
                    //}
                    ////  html = GetImgUrls(html);
                }
                else
                {
                    html = "";//这里其实应该做进一步处理的
                    ("频道  " + channelId + "   带分页的页面    " + url).Debug();
                }
            }
            else
            {
                html = "";
            }
            enSured = true;
            return html;
        }




        /// <summary>
        /// 得到字符串中的所有图片
        /// </summary>
        /// <param name="HTMLStr"></param>
        /// <returns></returns>
        public string DropImgs(string HTMLStr)
        {

            HTMLStr = Regex.Replace(HTMLStr, @"<img[\s\S]*?>", "", RegexOptions.IgnoreCase);

            //List<string> images = new List<string>();
            //Regex reg = new Regex(@"<img[\s\S]*?>", RegexOptions.IgnoreCase);
            //MatchCollection mc = reg.Matches(HTMLStr);
            //foreach (Match match in mc)
            //{
            //    images.Add(match.Groups["url"].Value.ToString().Replace("\"", ""));
            //}
            return HTMLStr;
        }
    }
}
